package com.farewell.blog.controller.admin;

import com.farewell.blog.mapper.UserMapper;
import com.farewell.blog.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * @author Farewell
 * @date 2021/2/15
 */
@Controller
public class LoginController {

    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/admin/login")
    public String login(HttpSession session) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = userMapper.getUserByUsername(authentication.getName());
        session.setAttribute("user", user);
        return "admin/blogs";
    }

    @GetMapping("/admin")
    public String admin() {
        return "admin/index";
    }

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @GetMapping("/logout")
    public String logout() {
        return "login";
    }
}
